package com.ogawa.jopai.devicemanage.activity


import android.graphics.Color
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
 import com.ogawa.base.base.BaseVMActivity
 import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.base.utils.DividerDecoration
import com.ogawa.base.utils.toPx
import com.ogawa.jopai.devicemanage.adapter.MessageListAdapter
import com.ogawa.jopai.devicemanage.bean.PutDeviceShareRequest
import com.ogawa.jopai.devicemanage.viewmodel.MessageViewModel
import com.ogawa.joypal.devicemanage.R
import com.ogawa.projectcommon.utils.DataStatusViewUtils
 import kotlinx.android.synthetic.main.activity_message.*


@Route(path = UrlPathConstant.ACTIVITY_DEVICEMANAGE_MESSAGE)
class MessageActivity : BaseVMActivity<MessageViewModel>() {
    var mAdapter = MessageListAdapter()
    override fun getLayoutId(): Int {
        return R.layout.activity_message
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.device_manage_message_title))
        initRv()
        swipeRefresh.setOnRefreshListener {
            swipeRefresh.isRefreshing = false
            mViewModel.getDeviceShare()
        }
        mViewModel.getDeviceShare()
    }



    private fun initRv(){
        messageRv.layoutManager = LinearLayoutManager(this)
        messageRv.setHasFixedSize(true)
        mAdapter.setOnItemClickListener { _, _, position ->

        }
        var emptyView = DataStatusViewUtils.getView(this,
            DataStatusViewUtils.DATA_EMPTY,getString(R.string.status_no_data), Color.TRANSPARENT,null)
        emptyView.setPadding(0,0,0,0)
        mAdapter.emptyView = emptyView
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val getDeviceShareResponse = mAdapter.data[position]
            var mPutDeviceShareRequest = PutDeviceShareRequest()
            mPutDeviceShareRequest.id = getDeviceShareResponse.id.toString()
            var messageStr = ""
            when(view.id){
                R.id.acceptIv->{
                    mPutDeviceShareRequest.status = "1"
                    messageStr = getString(R.string.accept_chair_share)
                }
                R.id.refuseIv->{
                    mPutDeviceShareRequest.status = "2"
                    messageStr = getString(R.string.refuse_chair_share)
                }
            }

//            AlertDialog.Builder(mActivity)
//                .setMessage(messageStr)
//                .setNegativeButton(R.string.cancel
//                ) { _, _ ->
//                }
//                .setPositiveButton(R.string.confirm){_, _ ->
//                    mViewModel.putDeviceShare(mPutDeviceShareRequest)
//                }
//                .show()
            mViewModel.putDeviceShare(mPutDeviceShareRequest)


        }
        messageRv.addItemDecoration(DividerDecoration(Color.parseColor("#55686868"), 2.toPx))
        messageRv.adapter = mAdapter
    }

    override fun providerVMClass(): Class<MessageViewModel>? {
        return MessageViewModel::class.java
    }

    override fun observeVM() {
        mViewModel.mGetDeviceShareResponseList.observe(this, Observer {
            it?.run {
                mAdapter.setNewData(it)
            }
        })
        mViewModel.mPutDeviceShareResponse.observe(this, Observer {
            mViewModel.getDeviceShare()
        })
    }
}